package com.food.eat.search.config;

import com.sun.org.apache.bcel.internal.generic.ARETURN;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@ConfigurationProperties(prefix = "")
public class ElasticsearchConfig {

    //es集群ip
    private String cluster_host;
    //es集群节点一端口
    private Integer eNode1_port;
    //es集群节点二端口
    private Integer eNode2_port;
    //es集群节点三端口
    private Integer eNode3_port;

    /**
     * 超时时间5分钟
      */
    private static final int TIME_OUT=5 * 60 * 1000;


    @Bean
     public RestClientBuilder restClientBuilder(){
        return  RestClient.builder(
          new HttpHost(cluster_host,eNode1_port,"http"),
          new HttpHost(cluster_host,eNode2_port,"http"),
          new HttpHost(cluster_host,eNode3_port,"http")
        );
     }



     @Bean(destroyMethod = "close")
     public RestHighLevelClient restHighLevelClient(@Autowired RestClientBuilder restClientBuilder){
           restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
               @Override
               public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                   return requestConfigBuilder.setSocketTimeout(TIME_OUT);
               }
           });

           return new RestHighLevelClient(restClientBuilder);
     }



}
